package com.juice.iot.sdk.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by haonan on 2019/1/29
 */
public class SysUtil {

    private static Logger log = LoggerFactory.getLogger(SysUtil.class);
    /**
     * 树莓派的revision版本号
     * @return
     */
    public static String getRevision(){
        return getResult("cat /proc/cpuinfo | grep 'Revision' | awk '{print $3}'");
    }

    /**
     * 树莓派序列号
     * @return
     */
    public static String getSn(){
        return getResult("cat /proc/cpuinfo | grep 'Serial' | awk '{print $3}'");
    }

    /**
     * cpu占内存
     * @return
     */
    public static String getCpuMemory(){
        return getResult("vcgencmd get_mem arm | grep arm | awk -F '=' '{print $2}'");
    }

    /**
     * gpu占内存
     * @return
     */
    public static String getGpuMemory(){
        return getResult("vcgencmd get_mem gpu | grep gpu | awk -F '=' '{print $2}'");
    }

    public static String getVolts(){
        return getResult("vcgencmd measure_volts core | awk -F '=' '{print $2}'");
    }

    public static String getSysTemp(){
        return getResult("vcgencmd measure_temp | awk -F '=' '{print $2}'");
    }

    private static String getResult(String command){
        ShellUtils.CommandResult result  = ShellUtils.
                execCommand(command,false);
        if(result.result == 0){
            return result.successMsg;
        }else{
            log.error("shell error: {}",result.errorMsg);
            throw new RuntimeException("shell error "+result.errorMsg);
        }
    }
}
